#pragma once
#include "../noncopyable.h"
#include <ucontext.h>
#include <functional>
#include <string>
#include <memory>
namespace TinyRPC{



class CoroutineUcontext : public Noncopyable, public std::enable_shared_from_this<CoroutineUcontext>{

public:
    using CoroutineTask = std::function<void()>;
    CoroutineUcontext(CoroutineTask coroutineTask, int stackSize=128*1024);
    void setName(const std::string& name);
    std::string getName();

    ~CoroutineUcontext();

    void runTask(){
        m_coroutineTask();
    }
    // 成员方法  
    void resume();
    void makeSureInMain();

    static void Yield();
    static void GetMainCoroutine();
    static bool InMain();
    static bool InCoroutine();
    static std::shared_ptr<CoroutineUcontext> CurrentCoroutine();
    
private:
    CoroutineUcontext();
    static void CoroutineFunc();

private:
    char* m_coroutineStack;
    int m_stackSize;
    ucontext_t m_uContext;
    CoroutineTask m_coroutineTask;
    std::string m_name;
};
using CoroutineUcontextPtr = std::shared_ptr<CoroutineUcontext>;

}